<?php
include "dbconn.php";

function curl($url) {
	$c = curl_init();
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($c, CURLOPT_URL, $url);
	$contents = curl_exec($c);
	curl_close($c);
	if ($contents) return $contents;
	else return false;
}

$url = 'http://pipes.yahoo.com/pipes/pipe.run?_id=694304adfdaddd901d02b319cc3ef29d&_render=json';
$data = json_decode(curl($url), true);
$items = $data[value][items];

for($i = 0; $i < sizeof($items); $i++) {
	$qry = "select max(id) as maxid from cartoon";
	$res = mysql_query($qry, $db);
	$row = mysql_fetch_array($res);
	$id = $row[maxid] + 1;
	
	$title = $items[$i][title];
	$pub_date = $items[$i][pubDate];
	$link = $items[$i][link];
	preg_match("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $items[$i][description], $result);
	$description = $items[$i][description];
	$img = $result[1];
	$utime = $items[$i]['y:published'][utime];

	$qry = "insert into cartoon set
	id =			'$id',
	title = 		'$title',
	pubDate = 		'$pub_date',
	link = 			'$link',
	description = 	'$description',
	img =		 	'$img',
	utime =			'$utime'
	";
	//echo $qry."<br />";
	mysql_query($qry, $db);
}

mysql_close();
?>